﻿using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace Extensions.Services;

public static class AddMiniProfiler
{
    public static IServiceCollection AddMiniProfilerSetup(this IServiceCollection services)
    {
        if (services == null) throw new ArgumentNullException(nameof(services));
        // 开发环境使用生产环境  不必使用
        services.AddMiniProfiler(options => { options.RouteBasePath = "/profiler"; }).AddEntityFramework();
        
        return services;
    }

    public static IApplicationBuilder UseMiniProfilerSetup(this IApplicationBuilder app, IHostEnvironment env)
    {
        // 只有在开发环境才使用miniprofile
        if (app == null) throw new ArgumentNullException(nameof(app));
        if (env == null) throw new ArgumentNullException(nameof(env));
        if (env.IsDevelopment())
        {
            app.UseMiniProfiler();
        }
        return app;
    }
}